package github.sf.fw.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.StringJoiner;

/**
 * 时间转换
 *
 * @author zhoup
 * @version 1.0.1
 */
public class Seconds {
    private final String[] units_ch = new String[]{"毫秒", "秒", "分", "时"};
    private final String[] units_en = new String[]{"ms", "s", "m", "h"};
    private final int[] unit_deltas = new int[]{0, 1000, 60, 60};
    private final boolean use_ch;

    public Seconds() {
        use_ch = false;
    }

    public Seconds(boolean use_ch) {
        this.use_ch = use_ch;
    }

    public static BigDecimal divide(Number a, Number b, RoundingMode roundingMode) {
        return BigDecimal.valueOf(a.doubleValue()).divide(BigDecimal.valueOf(b.doubleValue()), 2, roundingMode);
    }

    public String humanRead(long millis) {
        double v = millis;
        int i = 1;
        for (; i < unit_deltas.length; i++) {
            double temp = divide(v, unit_deltas[i], RoundingMode.FLOOR).doubleValue();
            if (temp < 1) {
                return v + getUnits(i - 1);
            }
            v = temp;
        }
        return v + getUnits(i - 1);
    }

    private String getUnits(int i) {
        if (use_ch) {
            return units_ch[i];
        }
        return units_en[i];
    }

    /**
     * 将秒转换为时:分:秒的格式
     *
     * @param seconds   秒
     * @param delimiter 分割符
     * @param isFull    是否补全
     */
    public String prettySeconds(long seconds, String delimiter, boolean isFull) {
        StringJoiner joiner = new StringJoiner(delimiter);
//        BigDecimal secondsOfSum = BigDecimal.valueOf(seconds);
        if (seconds < 60) {
            if (isFull) {
                return joiner.add("00").add("00").add(OnlySeconds(seconds, delimiter)).toString();
            }
            return OnlySeconds(seconds, delimiter);
        } else if (seconds < 3600) {
            if (isFull) {
                return joiner.add("00").add(HasMinute(seconds, delimiter)).toString();
            }
            return HasMinute(seconds, delimiter);
        } else {
            return HasHours(seconds, delimiter);
        }
    }

    /**
     * 只有秒，即seconds<60
     *
     * @param seconds 时间
     */
    public String OnlySeconds(long seconds, String delimiter) {
        if (seconds >= 60) {
            throw new RuntimeException("这里的秒不应该超过60");
        }
        StringJoiner joiner = new StringJoiner(delimiter);
        //秒
//        joiner.add("00").add("00");
        if (seconds < 10) {
            joiner.add("0" + seconds);
        } else {
            joiner.add(String.valueOf(seconds));
        }
        return joiner.toString();
    }

    /**
     * 有分钟，但没有小时
     *
     * @param seconds <3600
     */
    public String HasMinute(long seconds, String delimiter) {
        if (seconds >= 3600) {
            throw new RuntimeException("这里的秒不应该超过3600");
        }
        StringJoiner joiner = new StringJoiner(delimiter);
        long minute = seconds / 60;
        long seconds_s = seconds % 60;
        joiner.add(getPartStr(minute));
        joiner.add(getPartStr(seconds_s));
        return joiner.toString();
    }

    /**
     * 有小时
     *
     * @param seconds   时间
     * @param delimiter 分隔符
     */
    public String HasHours(long seconds, String delimiter) {
        if (seconds < 3600) {
            throw new RuntimeException("这里秒不应该小于3600");
        }
        StringJoiner joiner = new StringJoiner(delimiter);
        long hour = seconds / 3600;
        long minute_seconds = seconds % 3600;
        long minute = minute_seconds / 60;
        long second = minute_seconds % 60;
        joiner.add(getPartStr(hour));
        joiner.add(getPartStr(minute));
        joiner.add(getPartStr(second));
        return joiner.toString();
    }

    public String getPartStr(long n) {
        String result = "";
        if (n >= 60 || n < 0) {
            throw new RuntimeException("n不在[0,60]区间中");
        }
        if (n < 10) {
            result = "0" + n;
        } else {
            result = String.valueOf(n);
        }
        return result;
    }
}
